package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.core.Account;
import edu.chl.da.ejb.ctrl.IManageAccount;
import edu.chl.da.ejb.ctrl.IManageAgent;
import edu.chl.da.ejb.ctrl.IManageCustomer;
import java.security.cert.CertificateException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Kristofer
 *
 * Manage and store the current logged in user for the current session.
 * Handle login and logout events.
 *
 */

@ManagedBean(name = "LoginBean")
@SessionScoped
public class LoginBean {

    private Account currentAccount = null;
    private String accountName;
    private String password;
    private String authenticationLevel;

    private boolean loggedIn = false;
    private String returnPath;
    private String errorMsg;

    @EJB
    IManageAccount imAccount;

    @EJB
    IManageAgent imAgent;

    @EJB
    IManageCustomer imCustomer;

    /** Creates a new instance of LoginBean */
    public LoginBean() {
    }

    public void login(ActionEvent e) throws AbortProcessingException, CertificateException {

           if(accountName.equals("") || password.equals("")) {
                errorMsg = "Alla falt ifylda";
                returnPath = "";
                return;
           }
           else if (imAccount.validateAccountLogin(accountName, password)) {
               if(imAgent.getTravelagent(accountName) != null) {
                   authenticationLevel = "agent";
                   returnPath = "/admin/admin.jsf";
                   currentAccount = imAccount.getAccount(accountName);
                   loggedIn = true;
               }
               else if(imCustomer.getCustomer(accountName) != null) {
                   authenticationLevel = "customer";
                   returnPath = "/views/home.jsf";
                   currentAccount = imAccount.getAccount(accountName);
                   loggedIn = true;
               }
               else {
                    throw new CertificateException("no such customer or agent found, database is invalid or used wrong");
               }
           }
           else {
                errorMsg = "Ogiltiga inloggningsuppgifter, forsok igen!";
                returnPath = "";
                return;
           }
    }
    public String navigate() {
        if (loggedIn) {
            return returnPath + "?faces-redirect=true";
        }
        else {
           
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(errorMsg));
            return "/views/home.jsf?faces-redirect=true";
        }
    }

    public void logout(ActionEvent e) throws AbortProcessingException {
            this.loggedIn = false;
           
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            if (session != null) {
                session.invalidate();
            }          
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setCurrentAccount(Account currentAccount) {
        this.currentAccount = currentAccount;
    }

    public Account getCurrentAccount() {
        return currentAccount;
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }

    public void setAuthenticationLevel(String authenticationLevel) {
        this.authenticationLevel = authenticationLevel;
    }

    public String getAuthenticationLevel() {
        return authenticationLevel;
    }
}
